package com.mobisys.live.statistic;

import com.mobisys.live.bean.Tencent.StatisticTencentBean;
import com.mobisys.live.controller.statisticcontroller.TencentCloudController;
import com.mobisys.live.dao.mapper.LiveStatisticMapper;
import com.mobisys.live.dao.model.LiveStatistic;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.tencentcloud.LiveStatOutput;
import com.mobisys.live.response.tencentcloud.LiveStatResponse;
import com.mobisys.live.response.tencentcloud.OutputStreamInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.awt.*;
import java.util.List;
import java.util.TimerTask;

import static com.mobisys.live.config.ResponseCode.MSG_TENCENT_EVENT_ERROR;
import static com.mobisys.live.config.ResponseCode.TENCENT_EVENT_ERROR;

public class StatisticTask extends TimerTask {
    private  String inter;
    private StatisticTencentBean statisticTencentBean;
    private Response response = new Response();
    private StatisticTencent statisticTencent = new StatisticTencent();
    private Logger logger = LogManager.getLogger(StatisticTask.class.getName());
    private LiveStatisticMapper liveStatisticMapper;

    public StatisticTask(StatisticTencentBean statisticTencentBean,String infer){
        this.statisticTencentBean = statisticTencentBean;
        this.inter = infer;
    }
    @Override
    public void run() {
        System.out.println("-------设定要指定任务--------");
        try {
            response = statisticTencent.getStatistic(statisticTencentBean, inter);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        logger.debug("获取统计信息 ++++++++++++：" + response);

        if (response.getRetureData() == null) {
            System.out.println("++++++++++++++ message is null +++++++++++++++++++++++++++++++++++++++++++");
            logger.debug("response.getRetureData() is null");

        }


        //存储数据库
        //存储数据库
        LiveStatistic liveStatistic = new LiveStatistic();
        System.out.println("++++++++++++++ 4 message is ture start insert ++++++++++++");
        LiveStatResponse liveStatResponse = (LiveStatResponse) response.getRetureData();
        List<LiveStatOutput> liveStatOutput = liveStatResponse.getOutput();
        OutputStreamInfo outputStreamInfo = liveStatOutput.get(0).getStream_info().get(0);


        liveStatistic.setLiveId(outputStreamInfo.getStream_name().substring(5, 37));
        liveStatistic.setBandwidth(outputStreamInfo.getBandwidth());
        liveStatistic.setBps(outputStreamInfo.getSpeed());
        liveStatistic.setFps(outputStreamInfo.getFps());
        liveStatistic.setOnline(outputStreamInfo.getOnline());
        liveStatistic.setTime(outputStreamInfo.getTime());

        logger.debug("liveStatstic " + liveStatistic.toString());

        int k = liveStatisticMapper.insert(liveStatistic);
        if (k == 0) {
            response.setRetureValue(TENCENT_EVENT_ERROR);
            response.setRetureMsg(MSG_TENCENT_EVENT_ERROR);

        }
    }
    }

